#!/bin/sh

#################################################################
#  This program is the Confidential and Proprietary product of 
#  Cadence Design Systems.  Any unauthorized use, reproduction 
#  or transfer of this program is strictly prohibited. 
#  
#  Copyright (c) 1989 - 1995, Cadence Design Systems, Inc. 
#  All rights reserved. 
#  
#  Filename:  concept2alt
#  Purpose:   generates EDIF netlist from a Concept schematic
#  Author:    naji@cds9255.cadence.com
#  Date:      January 1995
#  
#  History:   
#	05/08/95 - 1.1a - dori - Add -i to hdlconfig call so
#		so that hdlconfig will ignore duplicate cell
#		occurances in multiple work files.
#################################################################

#
# set program variables
#

program="`basename $0`"
version="1.1a"
workdir="`pwd`"
cdsroot="${CDS_ROOT:-`cds_root $0`}"

#
# set the Altera library & map file paths
#

maxPffName="$program.pff"
lpmLibName="alt_lpm"
synLibName="alt_syn"
maxLibName="alt_max2"
maxLmfName="cadence.lmf"

#
# define function that is used to check whether a 5.X
# library is analyzed
#
 
chk_lib_path() {
 
	for file in `/bin/ls $1`; do
 
		if [ -d "$1/$file" ]; then
 
			if [ -d "$1/$file/hdl" ]; then
				return 0
			fi
		fi
 
	done
 
	return 1
}

#
# define function that is used get the path to a 5.X
# library
#
 
get_lib_path() {

	libname=$1
	libpath=$2
 
	if [ "$ALT_LIBS_PATH" ]; then
 
		p="$ALT_LIBS_PATH/$libname"
 
		if [ ! -d $p ]; then
			echo "Error: cannot find library - $p" >&2
			echo ">>> Check environment variable ALT_LIBS_PATH." >&2
			return 1
		fi
 
		chk_lib_path $p
		status=$?
 
		if [ "$status" -ne 0 ]; then
			echo "Error: invalid library - $p" >&2
			echo ">>> Library must be analyzed before it can be used." >&2
			return 1
		fi
 
		eval "$libpath=$p"
		return 0
	fi

	if [ "$ALT_HOME" ]; then
 
		p="$ALT_HOME/simlib/concept/$libname"
 
		if [ -d $p ]; then
 
			chk_lib_path $p
			status=$?
 
			if [ "$status" -eq 0 ]; then
				eval "$libpath=$p"
				return 0
			fi
		fi
	fi

	p="${cdsroot}/lib/$libname"
 
	if [ ! -d $p ]; then
		echo "Error: cannot find library - $p" >&2
		echo ">>> Check the current CDS hierarchy." >&2
		return 1
	fi
 
	chk_lib_path $p
	status=$?
 
	if [ "$status" -ne 0 ]; then
		echo "Error: invalid library - $p" >&2
		echo ">>> Library must be analyzed before it can be used." >&2
		return 1
	fi
 
	eval "$libpath=$p"
	return 0
}

#
# get path to Altera data files
#

if [ "$ALT_HOME" ]; then

	maxPffPath="${ALT_HOME}/cadence/bin/${maxPffName}"

	if [ ! -f $maxPffPath ]; then
		maxPffPath="${cdsroot}/share/library/altera/data/${maxPffName}"
	fi

	maxLmfPath="${ALT_HOME}/lmf/${maxLmfName}"

	if [ ! -f $maxLmfPath ]; then
		maxLmfPath="${cdsroot}/share/library/altera/data/${maxLmfName}"
	else
		maxLmfPath="${maxLmfName}"
	fi

else

	maxPffPath="${cdsroot}/share/library/altera/data/${maxPffName}"
	maxLmfPath="${cdsroot}/share/library/altera/data/${maxLmfName}"
fi

#
# define usage funtion
#
 
usage_exit() {
cat << EOF >&2
 
Usage: $program [-device name] [-family name] [-help] [-gcmd filename]
       [-genacf] [-log filename] [-rundir dirname] design

       -device name       Specify device name
       -family name       Specify family name
       -gcmd filename     Specify global command file (default: global.cmd)
       -genacf            Generate ACF file
       -help              Print usage message
       -log filename      Specify log file name (default: ${program}.log)
       -rundir dirname    Specify run directory name (default: altera.run)

EOF
exit 2
}

#
# define function to create full pathnames
#
 
full_path() {
 
if pathname=`dirname $1` ; then
	if filename=`basename $1` ; then
		pathname=`(cd $pathname; pwd)`
		echo $pathname/$filename
		return
	fi
fi
 
echo "Error: illegal file name - $1" >&2
exit 1
}

#
# process command-line arguments
#
 
while [ $# -gt 0 ]; do
 
	case $1 in

		-device)	if [ ! "$2" ]; then
						echo "Error: -device option requires an argument" >&2
						usage_exit
					fi

					device=$2 ; shift ; shift ;;

		-family)	if [ ! "$2" ]; then
						echo "Error: -family option requires an argument" >&2
						usage_exit
					fi

					family=$2 ; shift ; shift ;;

		-gcmd)		if [ ! "$2" ]; then
						echo "Error: -gcmd option requires an argument" >&2
						usage_exit
					fi

					gcmdfile=`full_path $2` ; shift; shift ;;

		-genacf)	genACF="yes" ; shift ;;

		-help)		usage_exit ;;

		-log)		if [ ! "$2" ]; then
						echo "Error: -log option requires an argument" >&2
						usage_exit
					fi

					logfile=`full_path $2` ; shift; shift ;;

		-rundir)	if [ ! "$2" ]; then
						echo "Error: -rundir option requires an argument" >&2
						usage_exit
					fi

					rundir=`full_path $2` ; shift ; shift ;;

		-*)			echo "Error: unrecognized option '$1'" >&2
					usage_exit ;;

		*)			design=$1 ; shift ;;

	esac

done

#
# check command-line arguments
#

if [ ! "$design" ]; then
	echo "Error: must specify design name" >&2
	usage_exit
fi

if [ ! "$rundir" ]; then
	rundir="${workdir}/altera.run"
fi

if [ ! "$logfile" ]; then
	logfile="${workdir}/${program}.log"
fi

if [ ! "$gcmdfile" ]; then
	gcmdfile="${workdir}/global.cmd"
fi

#
# print program and copyright information
#
 
echo "Running $program version $version - `date`." | tee $logfile
echo "Copyright (c) 1995 Cadence Design Systems. All Rights Reserved." | tee -a $logfile
echo "" | tee -a $logfile

#
# create run directory if it does not exists
#
 
if [ ! -d $rundir ]; then
 
	mkdir -p $rundir >/dev/null 2>&1
 
	if [ $? != 0 ]; then
		echo "Error: cannot create run directory - $rundir" >&2
		exit 1
	fi
fi

#
# create the design configuration
#

echo "Creating the design configuration ..."

echo "------------------------------------------------------------" >>$logfile
echo "" >>$logfile

options="-i -verilog -r ${design} -o ${rundir}/${design}.v -g ${gcmdfile} -a"

hdlconfig ${options} logic >>$logfile 2>&1

if [ $? != 0 ]; then
	echo ""
	echo "Error: Failed to create the design configuration."
	exit 1
fi

echo "" >>$logfile

#
# go to run directory
#
 
cd $rundir > /dev/null 2>&1

#
# go to run directory
#
 
cd $rundir > /dev/null 2>&1
 
#
# get path to the Altera 5.X libraries
#
 
get_lib_path ${lpmLibName} lpmLibPath
 
if [ "$?" -ne 0 ]; then
	exit 1
fi
 
get_lib_path ${synLibName} synLibPath
 
if [ "$?" -ne 0 ]; then
	exit 1
fi
 
get_lib_path ${maxLibName} maxLibPath
 
if [ "$?" -ne 0 ]; then
	exit 1
fi
 
#
# create the 5.X cds.lib file
#
 
echo "define $lpmLibName $lpmLibPath" > cds.lib
echo "define $maxLibName $maxLibPath" >> cds.lib
echo "define $synLibName $synLibPath" >> cds.lib
echo "define ${design}_lib ./${design}_lib" >> cds.lib

#
# analyze the design
#

echo "Analyzing the design ..."

echo "------------------------------------------------------------" >>$logfile
echo "" >>$logfile

van -lib ${design}_lib -globals glbl ${design}.v -command ${design}.vf >>$logfile 2>&1

if [ $? != 0 ]; then
	echo ""
	echo "Error: Failed to analyze the design."
	exit 1
fi

echo "" >>$logfile

#
# install properties files
#

for fpath in `cat ${design}.vf`; do

	vpath=`dirname $fpath`
	dpath=`dirname $vpath`
	
	drawing=`basename $dpath`

	if [ -f "${vpath}/viewprps.prp" ]; then

		ppath="./${design}_lib/${drawing}/hdl/viewprps.prp"

		sed 's/[(]View \"logic\"/\(View \"hdl\"/' ${vpath}/viewprps.prp > ${ppath} 2>>$logfile

		if [ $? != 0 ]; then
			echo ""
			echo "Error: Failed to install the property files."
			exit 1
		fi
	fi

done

#
# generate the Altera EDIF netlist
#

echo "Netlisting ..."

echo "------------------------------------------------------------" >>$logfile
echo "" >>$logfile

options="-lib ${design}_lib -map ${design}.map"
options="$options -nc -pinlist ${design}.pinlist"
options="$options -proff ${maxPffPath} -out ${design}.edf"

if [ "$genACF" ]; then
	options="$options -acf ${design}.acf -lmf ${maxLmfPath}"

	if [ "$device" ]; then
		options="$options -dev $device"
	fi

	if [ "$family" ]; then
		options="$options -fam $family"
	fi

else

	if [ "$device" ]; then
		echo ""
		echo "Warning: device name is ignored because -genacf option not specified"
	fi

	if [ "$family" ]; then
		echo ""
		echo "Warning: family name is ignored because -genacf option not specified"
	fi
fi


sir2alt $options $design >>$logfile 2>&1

if [ $? != 0 ]; then
	echo ""
	echo "Error: Failed to netlist the design."
	exit 1
fi

echo "" >>$logfile

#
# normal exit
#

echo "------------------------------------------------------------" >>$logfile
echo "" | tee -a $logfile
echo "Done." | tee -a $logfile
exit 0
